/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.apiGenerator;

import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.caf.generator.compileunit.CompileUnitGenerator;
import com.inspur.edp.caf.generator.compileunit.CompileUnitGeneratorContext;
import com.inspur.edp.cef.generator.apiGenerator.dataType.mgrAllInterface.MgrAllInterfaceGenerator;
import com.inspur.edp.cef.generator.apiGenerator.dataType.mgrInterface.MgrInterfaceGenerator;
import com.inspur.edp.cef.generator.apiGenerator.dataType.mgrInterface.mgrInfo.MgrInfoBaseGenerator;
import com.inspur.edp.cef.generator.apiGenerator.dataType.mgrInterface.resourceInfo.ModelInfoBaseGenerator;
import com.inspur.edp.cef.generator.apiGenerator.dataType.mgrInterface.MgrSingleInterfaceGenerator;
import com.inspur.edp.cef.generator.overall.CefGeneratorContext;

public abstract class CefApiGenerator extends CompileUnitGenerator {
  private CefGeneratorContext cefGenContext;

  public CefApiGenerator(CefGeneratorContext cefGenContext) {
    super();
    this.cefGenContext = cefGenContext;
    cefGenContext.setApiGenerator(this);
  }

  //	@Override
  //	protected final java.util.ArrayList<Assembly>  addReferences()
  //	{
  //		java.util.ArrayList<Assembly> list = super.addReferences();
  //		if (list == null)
  //		{
  //			list = new java.util.ArrayList<Assembly>();
  //		}
  //		//list.add(cefGenContext.ApiAssembly);
  //		//list.add(cefGenContext.EntityAssembly);
  //		return list;
  //	}

  @Override
  protected final CompileUnitGeneratorContext createProjectInfoContext() {
    CefApiGeneratorContext result = createCefApiGenContext();
    cefGenContext.setApiGenContext(result);
    return result;
  }

  @Override
  protected CefApiGeneratorContext getContext() {
    return (CefApiGeneratorContext) super.getContext();
  }

  protected CefApiGeneratorContext createCefApiGenContext() {
    return new CefApiGeneratorContext(cefGenContext);
  }

  @Override
  protected String getFilePath() {
    return cefGenContext.getMetadataJitContext().getCodePath();
  }

  //	@Override
  //	protected GenerateType  getGenerateType()
  //	{
  //		return GenerateType.LocalFile;
  //	}

  @Override
  protected final java.util.ArrayList<BaseGenerator> createChildGenerators() {
    java.util.ArrayList<BaseGenerator> list = super.createChildGenerators();
    if (list == null) {
      list = new java.util.ArrayList<BaseGenerator>();
    }
    // list.add(getEntityInterfaceGenerator());
    // list.add(getEntityAllInterfaceGenerator());
    if (getIsSingleInterface()) {
      list.add(getSingleMgrInterfaceGenerator());
    } else {
      list.add(getMgrInterfaceGenerator());
      list.add(getMgrAllInterfaceGenerator());
    }

    java.util.ArrayList<BaseGenerator> extend = getExtendChildGenerators();
    if (extend != null) {
      list.addAll(extend);
    }

    MgrInfoBaseGenerator  mgrInfoGen = createMgrInfoGenerator();
    if (mgrInfoGen != null) {
      list.add(mgrInfoGen);
    }

    ModelInfoBaseGenerator modelInfoGen = createModelInfoGenerator();
    if (modelInfoGen != null) {
      list.add(modelInfoGen);
    }

    return list;
  }

  protected java.util.ArrayList<BaseGenerator> getExtendChildGenerators() {
    return null;
  }

  protected abstract MgrInterfaceGenerator getMgrInterfaceGenerator();

  protected abstract MgrAllInterfaceGenerator getMgrAllInterfaceGenerator();

  protected MgrSingleInterfaceGenerator getSingleMgrInterfaceGenerator() {
    return null;
  }
  // why virtual? 变量不需要实现

  protected MgrInfoBaseGenerator createMgrInfoGenerator() {
    return null;
  }

  protected ModelInfoBaseGenerator createModelInfoGenerator(){return null;}
  /**
   * 是否精简生成Api
   *
   * @return 精简/不精简
   */
  protected boolean getIsSingleInterface() {
    return false;
  }

  // protected abstract EntityInterfaceGenerator  getEntityInterfaceGenerator();

  // protected abstract EntityAllInterfaceGenerator  getEntityAllInterfaceGenerator();

  @Override
  protected void afterGenerate() {
    super.afterGenerate();
    extendAfterGenerate();
  }

  public final void setCefGenCtxApiTypes() {
    //getContext().setAssembly(cefGenContext.getMetadataJitContext().getApiProjectAssembly);
    cefGenContext.setEntityInterfaceTypes(getContext().getEntityInterfaceTypes()); //
//     Context.Assembly.GetType(Context.EntityInterfaceType.getFullName());
    cefGenContext.setEntityAllInterfaceTypes(getContext().getEntityAllInterfaceTypes()); //
//     Context.Assembly.GetType(Context.EntityAllInterfaceType.getFullName());
    cefGenContext.setRootEntityInterfaceType(getContext().getEntityInterfaceTypes().get(this.cefGenContext.getCommonDataType().getCode()));
    if(getContext().getRootEntityAllInterfaceType()==null)
      cefGenContext.setRootEntityAllInterfaceType(getContext().getEntityAllInterfaceTypes().values().stream().findFirst().get());
    else
      cefGenContext.setRootEntityAllInterfaceType(getContext().getRootEntityAllInterfaceType());

    cefGenContext.setMgrInterfaceType(getContext().getMgrInterfaceType());

    cefGenContext.setMgrAllInterfaceType(getContext().getMgrAllInterfaceType());
  }

  protected void extendAfterGenerate() {
    // BE中的
    // beGenCtx.MgrAssFactoryInterfaceType =
    // Context.Assembly.GetType(Context.MgrActionAssFactoryInterfaceType.getFullName());
    // beGenCtx.BEAssFactoryInterfaceType =
    // Context.Assembly.GetType(Context.BEActionAssFactoryInterfaceType.getFullName());
  }

  @Override
  protected void beforeInitialize() {
    super.beforeInitialize();
    getContext().setPackageName(getPackageName());
//    cefGenContext.getMetadataJitContext().getApiModuleGenerator().addCompileUnitGenrator(this);
//    super.setRefProjectGen(cefGenContext.getMetadataJitContext().ApiGenerator);
  }
}
